package com.daigoudao.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class StringUtils {

	public static synchronized String getUUID() {
		String s = UUID.randomUUID().toString();
		return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18)
				+ s.substring(19, 23) + s.substring(24);
	}

	/**
	 * 根据时间字符串格式获取某一时间点的字符串
	 * 
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String formatDate(Date date, String formatStr) {
		DateFormat df = new SimpleDateFormat(formatStr);
		return df.format(date);
	}

	public static Date parse(String dateStr, String formatStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			return new Date();
		}
	}

	/**
	 * 把一个字符串变换为加密字符串,用于密码加密
	 */
	public static String pwdEncode(String str) {
		if (str == null)
			return null;
		byte[] barray = str.getBytes();
		if (barray == null)
			return null;

		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			barray = md.digest(barray);
		} catch (Exception e) {
			barray = str.getBytes();
		}

		StringBuffer desStr = new StringBuffer("");
		int y = barray.length / 3;
		int z = barray.length % 3;
		int i;
		for (i = 0; i < y; i++)
			encode64(barray, i * 3, 3, desStr);
		if (z > 0)
			encode64(barray, i * 3, z, desStr);
		return desStr.toString();
	}

	private static void encode64(byte[] bb, int index, int len, StringBuffer sb) {
		byte cc = 0;
		byte mm = 0x03;
		int i, j;
		for (i = 0, j = 2; i < len; i++, j += 2) {
			cc |= ((bb[i + index] & 0x0ff) >> j);
			append64(sb, cc);
			cc = (byte) (bb[i + index] & mm);
			cc <<= 4 - i * 2;
			mm <<= 2;
			mm |= 0x03;
		}
		append64(sb, cc);
		for (; i < 3; i++)
			append64(sb, (byte) 64);
	}

	private static void append64(StringBuffer sb, byte bb) {
		if (bb < 26)
			sb.append((char) (bb + 'A'));
		else if (bb < 52)
			sb.append((char) (bb - 26 + 'a'));
		else if (bb < 62)
			sb.append((char) (bb - 52 + '0'));
		else if (bb == 62)
			sb.append((char) '+');
		else if (bb == 63)
			sb.append((char) '!');
		else
			sb.append((char) '=');
	}

	/**
	 * 把一个字符串变换为加密字符串,长度32位,用于验证码加密
	 */
	public static String MD532(String str) {
		try {
			int i;
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			byte b[] = md.digest();
			StringBuffer buf = new StringBuffer("");

			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i += 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			return buf.toString(); // 32位的加密
		} catch (NoSuchAlgorithmException e) {
		}
		return "";
	}

	/**
	 * 用户验证码采用UUID+email进行加密
	 */
	public static String getUserValid(String email) {
		return MD532(UUID.randomUUID().toString() + email);
	}

	// 取得当前时间,格式为:yyyy-MM-dd HH:mm:ss
	public static String getCurrentTime() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(new Date());
	}

	public static synchronized String getCurrentTime(SimpleDateFormat sdf) {
		return sdf.format(new Date());
	}

	/**
	 * 获取文件后缀名
	 * 
	 * @param file
	 * @return
	 */
	public static String getFileSuffix(String fileName) {
		String suffix = "";
		int lastIndex = fileName.lastIndexOf(".");
		if (lastIndex != -1) {
			suffix = fileName.substring(lastIndex + 1);
		}
		return suffix;
	}

	public static void main(String[] args) {
		System.out.println(getUUID());
	}

}
